package com.xxx.video.dynamic.adapters;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.adapters.BaseViewHolder;
import com.xxx.video.commen.utils.CountUtils;
import com.xxx.video.commen.utils.DimensionUtil;
import com.xxx.video.commen.utils.ResourcesUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.mine.beans.DynamicListBean;

import butterknife.BindView;

public class DynamicListAdapter extends BaseRecycleViewAdapter<DynamicListBean.PageBean.ListBean, BaseViewHolder> {



    public DynamicListAdapter(@NonNull Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder onCreateDefaultViewHolder(View view, int viewType) {
        if (viewType == 0) {
            /*
             * audio
             */
            return new MyAudioHolder(view);
        } else if (viewType == 1) {
            /*
             * video
             */
            return new MyVideoHolder(view);
        }
        return new MyAudioHolder(view);
    }

    @Override
    public int getItemLayout(int viewType) {
        if (viewType == 0) {
            /*
             * audio
             */
            return R.layout.item_dynamic_list_audio;
        } else if (viewType == 1) {
            /*
             * video
             */
            return R.layout.item_dynamic_list_video;
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).fType;
    }

    public class MyAudioHolder extends BaseViewHolder {

        @BindView(R.id.iv_item_dynamic_list_audio_icon)
        ImageView ivItemDynamicListAudioIcon;
        @BindView(R.id.tv_item_dynamic_list_audio_title)
        TextView tvItemDynamicListAudioTitle;
        @BindView(R.id.tv_item_dynamic_list_audio_time)
        TextView tvItemDynamicListAudioTime;

        public MyAudioHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData2View(int position) {
            DynamicListBean.PageBean.ListBean listBean = getItem(position);
            /*
             * title
             */
            tvItemDynamicListAudioTitle.setText(StringUtils.getInstance().getNotEmptyMsg(listBean.fTitle));
            /*
             * time
             */
            tvItemDynamicListAudioTime.setText(StringUtils.getInstance().getNotEmptyMsg(String.valueOf(listBean.fCreateTime)));
        }
    }

    public class MyVideoHolder extends BaseViewHolder {

        @BindView(R.id.iv_item_dynamic_list_video_avatar)
        ImageView ivItemDynamicListVideoAvatar;
        @BindView(R.id.tv_item_dynamic_list_video_name)
        TextView tvItemDynamicListVideoName;
        @BindView(R.id.iv_item_dynamic_list_video_cover)
        ImageView ivItemDynamicListVideoCover;
        @BindView(R.id.tv_item_dynamic_list_video_cover_play_count)
        TextView tvItemDynamicListVideoCoverPlayCount;
        @BindView(R.id.tv_item_dynamic_list_video_cover_comment_count)
        TextView tvItemDynamicListVideoCoverCommentCount;
        @BindView(R.id.tv_item_dynamic_list_video_title)
        TextView tvItemDynamicListVideoTitle;
        @BindView(R.id.tv_item_dynamic_list_video_time)
        TextView tvItemDynamicListVideoTime;

        public MyVideoHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData2View(int position) {
            DynamicListBean.PageBean.ListBean listBean = getItem(position);
            /*
             * avatar
             */
            Glide.with(context).load(ResourcesUtils.getInstance().getImagePath(listBean.fUsePicurl))
                    .apply(new RequestOptions()
                            .error(R.drawable.icon_default_avatar)
                            .placeholder(R.drawable.icon_default_avatar)
                            .circleCrop())
                    .into(ivItemDynamicListVideoAvatar);
            /*
             * name
             */
            tvItemDynamicListVideoName.setText(StringUtils.getInstance().getNotEmptyMsg(listBean.fUserName));
            /*
             * title
             */
            tvItemDynamicListVideoTitle.setText(StringUtils.getInstance().getNotEmptyMsg(listBean.fTitle));
            /*
             * cover
             */
            Glide.with(context).load(ResourcesUtils.getInstance().getImagePath(listBean.fPicurl))
                    .apply(new RequestOptions()
                            .error(R.drawable.bg_place_holder_nine_five)
                            .placeholder(R.drawable.bg_place_holder_nine_five)
                            .transform(new CenterCrop(), new RoundedCorners(DimensionUtil.dpToPx(5))))
                    .into(ivItemDynamicListVideoCover);
            /*
             * play count
             */
            tvItemDynamicListVideoCoverPlayCount.setText(CountUtils.getInstance().getCount(listBean.fPlayTimes));
            /*
             *comment count
             */
            tvItemDynamicListVideoCoverCommentCount.setText(CountUtils.getInstance().getCount(listBean.fCommentTimes));
            /*
             * time
             */
            tvItemDynamicListVideoTime.setText(StringUtils.getInstance().getNotEmptyMsg(String.valueOf(listBean.fCreateTime)));
        }
    }
}
